package org.luckyjourney.service.plan.impl;

import org.luckyjourney.entity.booking.Orders;
import org.luckyjourney.entity.plan.AppointmentRecords;
import org.luckyjourney.entity.plan.dto.AppointmentRecordsDTO;
import org.luckyjourney.entity.plan.options;
import org.luckyjourney.entity.plan.vo.AppointmentRecordsVO;
import org.luckyjourney.mapper.booking.OrdersMapper;
import org.luckyjourney.mapper.plan.AppointmentRecordsMapper;
import org.luckyjourney.service.plan.AppointmentRecordsService;
import org.luckyjourney.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppointmentRecordsServiceImpl implements AppointmentRecordsService {
    @Autowired
    AppointmentRecordsMapper appointmentRecordsMapper;
    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public List<AppointmentRecordsVO> selectPlan(AppointmentRecords appointmentRecords) {
        String ordersNumber = appointmentRecords.getOrdersNumber();
        Orders orders = ordersMapper.selectOrdersNumber(ordersNumber);
        //根据订单查看出预约的表单
        Long userId = orders.getUserId();
        //获取咨询的类型
        Long classifyId = orders.getClassifyId();
        AppointmentRecords appointmentRecords1 = new AppointmentRecords();
        appointmentRecords1.setConsultantsId(userId);
        appointmentRecords1.setTypeId(classifyId);
        List<AppointmentRecordsDTO> appointmentRecordsDTOS = appointmentRecordsMapper.selectAppointment(appointmentRecords1);
        //获取下拉框
        List<options> options = appointmentRecordsMapper.selectTypeId(classifyId);
        ArrayList<AppointmentRecordsVO> list = new ArrayList<>();
        for (AppointmentRecordsDTO appointmentRecordsDTO : appointmentRecordsDTOS) {
            //根据类型查询预约表单
            String type = appointmentRecordsDTO.getType();
            AppointmentRecordsVO recordsVO = new AppointmentRecordsVO();
            for (org.luckyjourney.entity.plan.options option : options) {
                Integer markId = option.getMarkId();
                //根据类型分类查询是否是单选还是复选框
                if (type.equals("radio") && markId == 1) {
                    List<options> optionsList = appointmentRecordsMapper.selectTypeMarkId(markId);
                    recordsVO.setOptionsList(optionsList);
                    recordsVO.setEnglish(appointmentRecordsDTO.getEnglish());
                    recordsVO.setName(appointmentRecordsDTO.getName());
                    recordsVO.setType(appointmentRecordsDTO.getType());
                    recordsVO.setId(appointmentRecordsDTO.getId());
                    recordsVO.setPlaceholders(appointmentRecordsDTO.getPlaceholders());
                    recordsVO.setConsultantsId(appointmentRecordsDTO.getConsultantsId());
                    recordsVO.setTypeId(appointmentRecordsDTO.getTypeId());
                    recordsVO.setStatus(appointmentRecordsDTO.getStatus());
                } else if (type.equals("checkbox") && markId == 2) {
                    List<options> optionsList = appointmentRecordsMapper.selectTypeMarkId(markId);
                    recordsVO.setOptionsList(optionsList);
                    recordsVO.setEnglish(appointmentRecordsDTO.getEnglish());
                    recordsVO.setName(appointmentRecordsDTO.getName());
                    recordsVO.setType(appointmentRecordsDTO.getType());
                    recordsVO.setId(appointmentRecordsDTO.getId());
                    recordsVO.setPlaceholders(appointmentRecordsDTO.getPlaceholders());
                    recordsVO.setConsultantsId(appointmentRecordsDTO.getConsultantsId());
                    recordsVO.setTypeId(appointmentRecordsDTO.getTypeId());
                    recordsVO.setStatus(appointmentRecordsDTO.getStatus());
                }
            }

            recordsVO.setEnglish(appointmentRecordsDTO.getEnglish());
            recordsVO.setName(appointmentRecordsDTO.getName());
            recordsVO.setType(appointmentRecordsDTO.getType());
            recordsVO.setId(appointmentRecordsDTO.getId());
            recordsVO.setPlaceholders(appointmentRecordsDTO.getPlaceholders());
            recordsVO.setConsultantsId(appointmentRecordsDTO.getConsultantsId());
            recordsVO.setTypeId(appointmentRecordsDTO.getTypeId());
            recordsVO.setStatus(appointmentRecordsDTO.getStatus());
            list.add(recordsVO);
        }

        return list;
    }

}
